//
//  XJGoodsDetailViewController.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/11.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJGoodsDetailViewController.h"
#import "XJGoodsDetailBaseViewController.h"
#import "XJShopDetailViewController.h"//店铺详情
@interface XJGoodsDetailViewController ()<UIScrollViewDelegate>

@property (nonatomic ,strong) UIButton * popBtn;
@property (nonatomic ,strong) UIButton * shareBtn;

@property (strong, nonatomic) UIScrollView *scrollerView;

/**根据goodsId获取商品详情*/
@property (nonatomic, copy) NSString *goodsId;
@end

@implementation XJGoodsDetailViewController
+ (void)goodsDetailWithController:(UIViewController *)controller goodsType:(GoodsDetailType)type goodsId:(NSString *)goodsId {
    
    XJGoodsDetailViewController * xjgdVC = [[XJGoodsDetailViewController alloc]init];
    xjgdVC.type = type;
    xjgdVC.goodsId = goodsId;
    XJLog(@"goodsId:%@",goodsId)
    
    [controller.navigationController pushViewController:xjgdVC animated:YES];
}

- (UIScrollView *)scrollerView
{
    if (!_scrollerView) {
        _scrollerView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _scrollerView.frame = self.view.bounds;
        _scrollerView.showsVerticalScrollIndicator = NO;
        _scrollerView.showsHorizontalScrollIndicator = NO;
        _scrollerView.pagingEnabled = YES;
        _scrollerView.bounces = NO;
        _scrollerView.delegate = self;
        [self.view addSubview:_scrollerView];
    }
    return _scrollerView;
}

- (UIButton *)popBtn{
    if (!_popBtn) {
        UIButton * popBtn = [UIButton new];
        self.popBtn = popBtn;
        [self.view addSubview:popBtn];
    }
    return _popBtn;
}

- (UIButton *)shareBtn{
    if (!_shareBtn) {
        UIButton * shareBtn = [UIButton new];
        self.shareBtn = shareBtn;
        [self.view addSubview:shareBtn];
    }
    return _shareBtn;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"商品详情";
    self.view.backgroundColor =WHITE_COLOR;
    switch (_type) {
        case GoodsDetailTypeSecKill:
            XJLog(@"秒杀商品 goodId:%@",self.goodsId);
            break;
        case GoodsDetailTypeRecommend:
            XJLog(@"推荐商品 goodId:%@",self.goodsId);
            break;
        case GoodsDetailTypeNormal:
            XJLog(@"普通商品 goodId:%@",self.goodsId);
            break;
        case GoodsDetailTypeOther:
            XJLog(@"其他商品 goodId:%@",self.goodsId);
            break;
        default:
            break;
    }
    [self setUpChildViewControllers];

    [self initSubview];
    
    [self setNavBarButton];
    
    [self addChildViewController];

    [self setBottomButton];
    
    [self requestGoodsInfo];

}

#pragma mark - 获取数据
- (void)requestGoodsInfo {
    [XJHttpTool requestGoodsInfo:self.goodsId successHandler:^(id responseObjc) {
        
        XJLog(@"商品详情请求数据成功");
    } failureHandler:^(NSError *error) {
        XJLog(@"商品详情 失败");
    }];
}
- (void)initSubview{
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.scrollerView.backgroundColor = self.view.backgroundColor;
    self.scrollerView.contentSize = CGSizeMake(self.view.width * self.childViewControllers.count, 0);
    self.automaticallyAdjustsScrollViewInsets = NO;
}

#pragma mark - 添加子控制器View
-(void)addChildViewController
{
    NSInteger index = _scrollerView.contentOffset.x / _scrollerView.width;
    UIViewController *childVc = self.childViewControllers[index];
    
    if (childVc.view.superview) return; //判断添加就不用再添加了
    childVc.view.frame = CGRectMake(index * _scrollerView.width, 0, _scrollerView.width, _scrollerView.height);
    [_scrollerView addSubview:childVc.view];
    
}

#pragma mark - 添加子控制器
-(void)setUpChildViewControllers{
    //XJWeakSelf(self);
    XJGoodsDetailBaseViewController * goodBaseVc = [[XJGoodsDetailBaseViewController alloc] init];
    [self addChildViewController:goodBaseVc];
}

#pragma mark - <UIScrollViewDelegate>
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self addChildViewController];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    NSInteger index = scrollView.contentOffset.x / scrollView.width;
//    UIButton *button = _bgView.subviews[index];
//
//    [self topBottonClick:button];
    
    [self addChildViewController];
}

#pragma mark - 顶部按钮
- (void)setNavBarButton{
    
    self.popBtn.frame = CGRectMake(10, 30, 30, 30);
    self.popBtn.layer.masksToBounds = YES;
    self.popBtn.layer.cornerRadius = 15;
    self.popBtn.backgroundColor = XJCOLOR_RGBA(0, 0, 0, 0.3);
    [self.popBtn setImage:[UIImage imageNamed:@"nav_back_w"] forState:UIControlStateNormal];
    [self.popBtn addTarget:self action:@selector(popBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.shareBtn.frame = CGRectMake(KWidth - 40, 30, 30, 30);
    self.shareBtn.layer.masksToBounds = YES;
    self.shareBtn.layer.cornerRadius = 15;
    self.shareBtn.backgroundColor = XJCOLOR_RGBA(0, 0, 0, 0.3);
    [self.shareBtn setImage:[UIImage imageNamed:@"nav_share"] forState:UIControlStateNormal];
    [self.shareBtn addTarget:self action:@selector(shareBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark - 底部按钮
- (void)setBottomButton{
    
    UIImageView *topLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, KHeight-55, KWidth, 5)];
    topLine.image = [UIImage imageNamed:@"tapbar_top_line"];
    [self.view addSubview:topLine];
    
    [self setBottomLeftButton];
    
    [self setBottomRightButton];
    
}

#pragma mark - 店铺 客服 收藏
- (void)setBottomLeftButton{
    NSArray * leftBtnArray = @[@[@"doods_shop",@"店铺"],
                              @[@"goods_kefu",@"客服"],
                              @[@"goods_shoucang_off",@"收藏"]];
    //CGFloat buttonW = KWidth * 0.2;
    CGFloat buttonH = 50;
    CGFloat buttonY = KHeight - buttonH;
    CGFloat leftBtn_margin = UI_IS_IPHONE5 ? 10 : 20;
    
    [leftBtnArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        leftButton.backgroundColor = WHITE_COLOR;
        leftButton.frame = CGRectMake(ADAPTWidth(14)+idx*(24 + leftBtn_margin), buttonY, 24, 50);
        leftButton.tag = idx;
        [leftButton buttonWithTitle:[obj lastObject] titleFont:XJFONTSET_R(10) titleColor:XJCOLOR_RGB(121, 130, 133) imageType:ImageTypeImg imageName:[obj firstObject] withTarget:self action:@selector(leftButtonClick:) style:ImgTextBtnStyleTop margin:5];
        [self.view addSubview:leftButton];
    }];
    
}

#pragma mark - 加入购物车 立即购买
- (void)setBottomRightButton{
    
    NSArray * rightArray = @[@"加入购物车",@"立即购买"];
    CGFloat buttonH = 35;
    CGFloat buttonW = ADAPTWidth(110);
    CGFloat buttonY = KHeight - 50 + 7.5;
    
    [rightArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        rightButton.frame = CGRectMake(ADAPTWidth(133)+idx*(buttonW+10), buttonY, buttonW, buttonH);
        rightButton.tag = idx;
        [rightButton setTitle:rightArray[idx] forState:UIControlStateNormal];
        [rightButton setTitleColor:XJCOLOR_RGB(17, 17, 17) forState:UIControlStateNormal];
        rightButton.titleLabel.font = XJFONTSET_R(15);
        rightButton.backgroundColor = (idx==0)? WHITE_COLOR : MAIN_COLOR;
        rightButton.layer.masksToBounds = YES;
        rightButton.layer.cornerRadius = buttonH/2;
        rightButton.layer.borderWidth = 0.5;
        rightButton.layer.borderColor = (idx==0)?XJCOLOR_RGB(121, 130, 133).CGColor:WHITE_COLOR.CGColor;
        [rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:rightButton];
    }];
}

#pragma mark - 顶部按钮点击
- (void)popBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)shareBtnClick{
    
    XJLog(@"share");
}

#pragma mark - 底部按钮点击
- (void)leftButtonClick:(UIButton *)btn{
    if (btn.tag == 0) {
        XJLog(@"tag=%ld 点击了店铺按钮",(long)btn.tag);
        XJShopDetailViewController *shopDetailVC = [[XJShopDetailViewController alloc] init];
        
        // 5-4 接口叫接口吗？
        shopDetailVC.shopId = @"5"; // 店铺id 目前只有这个接口有点数据
        
        [self presentViewController:shopDetailVC animated:YES completion:nil];
    } else if (btn.tag == 1) {
        XJLog(@"tag=%ld 点击客服按钮",(long)btn.tag);
    } else if (btn.tag == 2){
        XJLog(@"tag=%ld 点击收藏按钮",(long)btn.tag);
    }
}
- (void)rightButtonClick:(UIButton *)btn{
    XJLog(@"btn %ld",(long)btn.tag);
    
    //异步发通知
    dispatch_sync(dispatch_get_global_queue(0, 0), ^{
        NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%zd",btn.tag],@"buttonTag", nil];
        [[NSNotificationCenter defaultCenter]postNotificationName:SELECTCARTORBUY object:nil userInfo:dict];
    });
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
